package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.DictTypeServiceClient;
import com.hxkj.domain.enums.ArticleExceptionEnum;
import com.hxkj.domain.enums.DictItemExceptionEnum;
import com.hxkj.domain.po.DictType;
import com.hxkj.service.DictTypeService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DictITypeServiceImpl implements DictTypeService {

    @Autowired
    private DictTypeServiceClient dictTypeServiceClient;


    @Override
    public PageInfo<DictType> getDictTypeList(DictType dictType, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(20);
        }
        ParameterDataStruct<DictType> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(dictType);
        parameterDataStruct.setPageTo(pageParam);
        ResultDataStruct<PageInfo> resultDataStruct = dictTypeServiceClient.getlist(parameterDataStruct);
        if(resultDataStruct.getCode() == 200){
            return resultDataStruct.getData();
        }
        throw new CustomMessageException(resultDataStruct.getCode(),resultDataStruct.getMessage());

    }

    @Override
    public DictType getDictTypeFromCache(DictType dictType) throws Exception {
        ResultDataStruct<DictType> result;
        if (dictType.getDictTypeId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        try {
            result = dictTypeServiceClient.get(dictType);
            if (result.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询一条一级字典失败:{}", e);
            throw e;
        }
        return result.getData();
    }

    @Override
    public void updateDictType(DictType dictType) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        if (dictType.getDictTypeId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        if (dictType.getDictName() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_NAME);
        }
        if (dictType.getDictTypeCode() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (dictType.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        dictType.setUpdateTime(System.currentTimeMillis());
        try {
            //修改字典一级字典
            result = dictTypeServiceClient.updateDictType(dictType);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改一级 字典失败:{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public void deleteDictType(DictType dictType) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        if (dictType.getDictTypeId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        try {
            //删除一级字典
            result = dictTypeServiceClient.deleteDictType(dictType);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除一级 字典失败:{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public void saveDictType(DictType dictType) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        if (dictType.getDictName() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_NAME);
        }
        if (dictType.getDictName() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_ITEMVALUE_NULL);
        }
        if (dictType.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        dictType.setCreateTime(System.currentTimeMillis());
        try {
            //新增一级字典
            result = dictTypeServiceClient.saveDictType(dictType);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增一级 字典失败:{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public void modifyDictType(DictType dictType) throws Exception {
        ResultMessageStruct result = new ResultMessageStruct();
        if (dictType.getDictTypeId() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTITEM_NOT_ID);
        }
        if (dictType.getDelTag() == null) {
            throw new CustomException(DictItemExceptionEnum.DICTTYP_DEL_TAG_NULL);
        }
        try {
            //禁用一级字典
            result = dictTypeServiceClient.modifyDictType(dictType);
            if (result.getCode() != null) {
                throw new CustomException(DictItemExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
